<?php
// media gallery filters
function dt_media_upload_tab_filter($tabs) {
	if( 'dt_gallery' == get_post_type($_REQUEST['post_id']) ) {
		global $wpdb;
        
        if( isset($tabs['library']) ) {
			unset($tabs['library']);
		}
		
        if( isset($tabs['gallery']) ) {
			unset($tabs['gallery']);
		}
        
        if( isset($tabs['type_url']) ) {
			unset($tabs['type_url']);
		}
        
        $post_id = intval($_REQUEST['post_id']);
  
        if ( $post_id ) {
            $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
        }
        
        if ( empty($attachments) ) {
            unset($tabs['gallery']);
            return $tabs;
        }
    
		if( !isset($tabs['dtmadia'])) {
			$tabs['dtmedia'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
		}
        
        if( isset($tabs['type']) ) {
            $tabs['type'] = 'Upload Images';
        }
	}
	return $tabs;
}
add_filter('media_upload_tabs', 'dt_media_upload_tab_filter', 99 );

function dt_attachment_fields_to_edit_filter($fields, $post) {
	if( 'dt_gallery' == get_post_type($post->post_parent) ) {
        unset($fields['align']);
        unset($fields['image-size']);
        unset($fields['post_content']);
        unset($fields['image_alt']);
        unset($fields['url']);
	}
	return $fields;
}
add_filter('attachment_fields_to_edit', 'dt_attachment_fields_to_edit_filter', 99, 2);
// media filters end

// some little filters for posts links
function dt_next_posts_link_attr( $attr ) {
    $attr .= sprintf('class="%s"', 'rarr');
    return $attr;
}

function dt_previous_posts_link_attr( $attr ) {
    $attr .= sprintf('class="%s"', 'larr');
    return $attr;
}
//---

// first cool letter filter!
function dt_first_letter_filter( $str ) {
    if( isset($str[0]) ) {
        $first_lettr = '<span class="fl">'. $str[0]. '</span>';
        $str = substr($str, 1);
        $str = $first_lettr. $str;
    }
	return $str;
}
add_filter( 'widget_title', 'dt_first_letter_filter' );

// post parents filter
function dt_posts_parents_where( $where ) {
	global $wpdb;

	if( dt_get_where_filter_params() ) {
        $where .= sprintf(
            " AND %s.post_parent IN(%s)",
            $wpdb->posts,
            dt_get_where_filter_params()
        );
	}
    
    return $where;
}

// excerpts details
function new_excerpt_more($more) {   
/*	global $post;
    if( 'post' == $post->post_type || 'dt_portfolio' == $post->post_type ) {
        $more = sprintf(
            '<div class="pages"><a href="%s" class="more-link"><span>Details<i></i></span></a></div>',
            get_permalink($post->ID)
        );
    }else {
        $more = '';
    }
*/    
    return '';//$more;
}
add_filter('excerpt_more', 'new_excerpt_more');

// excerpts details in portfolio GRIDDD
function dt_no_excerpt_more($more) {
    return '';
}

// excerpt with password form
function dt_password_excerpt($excerpt) {
    if ( post_password_required() )
        $excerpt = get_the_password_form();
    return $excerpt;
}

// password form filter for gallery
function dt_gallery_passform() {
    global $post;
    $label = 'pwbox-'.( empty( $post->ID ) ? rand() : $post->ID );
    $o = '<form class="protected-post-form" action="' . get_option( 'siteurl' ) . '/wp-pass.php" method="post"><label for="' . $label . '">' . __( "Password:" ) . ' </label><input name="post_password" id="' . $label . '" type="password" size="20" /><input type="submit" name="Submit" value="' . esc_attr__( "Submit" ) . '" />
    </form>
    ';
    return $o;
}

// content link
function dt_content_link_filter( $link ) {   
    $link = sprintf(
        '<div class="pages">%s</div>',
        $link
    );

    return $link;
}
add_filter('the_content_more_link', 'dt_content_link_filter');

// gallery shortcode filter
function dt_filter_gallery_sc($output, $attr) {
	global $post, $wp_locale;
	$exclude_def = '';
    $event = '';
	if( $hide_in_gal = get_post_meta( $post->ID, 'hide_in_gal', true ) && ('gallery' == get_post_format($post->ID)) ) {
		$exclude_def = get_post_thumbnail_id( $post->ID );
	}
	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
	if ( isset( $attr['orderby'] ) ) {
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
		if ( !$attr['orderby'] )
			unset( $attr['orderby'] );
	}
		
	extract(shortcode_atts(array(
		'order'      => 'ASC',
		'orderby'    => 'menu_order ID',
		'id'         => $post->ID,
		'itemtag'    => 'li',
		'columns'    => 3,
		'size'       => 'thumbnail',
		'include'    => '',
		'exclude'    => $exclude_def
	), $attr));

	$id = intval($id);
	if ( 'RAND' == $order )
		$orderby = 'none';

	if ( !empty($include) ) {
		$include = preg_replace( '/[^0-9,]+/', '', $include );
		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

		$attachments = array();
		foreach ( $_attachments as $key => $val ) {
			$attachments[$val->ID] = $_attachments[$key];
		}
	} elseif ( !empty($exclude) ) {
		$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	} else {
		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
	}

	if ( empty($attachments) )
		return '';

	if ( is_feed() ) {
		$output = "\n";
		foreach ( $attachments as $att_id => $attachment )
			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
		return $output;
	}

	if( isset($attr['link']) && ('file' == $attr['link']) ) {
        $event .= 'onclick="return hs.expand(this, { slideshowGroup: \'' . $post->ID . '\' })"';
		$hs_class = " hs_me";
	}else {
		$hs_class = " to_attachment";
	}
	
	$itemtag = tag_escape($itemtag);
	$columns = intval($columns);
	$size_class = sanitize_html_class( $size );
	
	$output = "<ul class='gall_std{$hs_class} gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";

	$i = 0;
	foreach ( $attachments as $id => $attachment ) {
		$class = $description = '';
//        $class .= 'highslide ';
		if( isset($attr['link']) && ('file' == $attr['link']) ) {
			$href = wp_get_attachment_image_src($id, 'full');
			$href = $href?current($href):'#';
			$class .= "fadeThis ";
		}else {
			$href = get_permalink($id);
		}
		
		if( $attachment->post_content ) {
			$description = '<p class="wp-caption-text">'.wptexturize($attachment->post_content).'</p>';
			$class .= 'wp-caption ';
		}
		
		$caption = wptexturize(trim($attachment->post_excerpt));
		$dt_img = dt_get_thumbnail(
            array(
                'img_id'	=> $id,
                'width'		=> 125,
                'height'    => 125,
                'upscale'	=> true,
                'quality'   => 100,
                'zc_align'  => 'c'
            )        
        );
        $src[0] = $dt_img['thumnail_img'];
        $src[1] = $dt_img['width'];
        $src[2] = $dt_img['height'];
        //$src = wp_get_attachment_image_src($id, $size);
        
		$link = "<a class='{$class}' href='{$href}' title='{$caption}' {$event}>
		<img src='{$src[0]}' alt='' width='{$src[1]}' height='{$src[2]}'/><i></i>{$description}
		</a>";
        
        $li_size = $src[1] + 2;
        
		$output .= "<{$itemtag} class='shadow_light gallery-item' style='width: {$li_size}px;'>";
		$output .= $link;
		$output .= "</{$itemtag}>";
	}

	$output .= "</ul>\n";

	return $output;
}
add_filter('post_gallery', 'dt_filter_gallery_sc', 10, 2);

// add some class to image when insert it to editor
function dt_image_chool_frame( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
    $html = str_replace( $align, $align . ' frame-img', $html );
    return $html;
}
add_filter( 'image_send_to_editor', 'dt_image_chool_frame', 10, 8 );

// filter pagelink when ajaxing paginatior
function dt_ajax_paginator_filter( $href ) {
    $data = dt_get_where_filter_params();
    $first = true;
    
    $search = array(
        '&paged=',
        '?paged=',
        '/page/'
    );
    
    foreach( $search as $exp ) {
        $str = explode( $exp, $href );
    
        if( isset($str[1]) ) {
            $href = '#' . $data['slug'] . '/' . $str[1];
            $first = false;
            break;
        }
    }
    
    if( $first ) {
        $href = '#' . $data['slug'] . '/' . 1;
    }
       
    if( !empty($data['ajaxurl']) && !empty($data['pageurl']) ) {
        $href = str_replace( $data['ajaxurl'], $data['pageurl'], $href );
    }
    
    return $href;
}

// admin interface filters

function dt_shortcbuilder_photos_ppp_filter( $data ) {
    $data['before'] = '<fieldset style="padding-left: 15px;"><legend>Number of items to display:</legend><label>';
    $data['after'] = '%2$s</label></fieldset>';
    $data['description'] = '';
    return $data;
}

function dt_shortcbuilder_photos_orderby_filter( $data ) {
    $data['before'] = '<legend>Ordering settings:</legend>' . $data['before'];
    return $data;
}

function dt_shortcbuilder_photos_order_filter( $data ) {
    $data['before'] = '<legend>Sort by:</legend>' . $data['before'];
    return $data;
}

/*
Plugin Name: Shortcode Empty Paragraph Fix
Plugin URI: http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/
Description: Fix issues when shortcodes are embedded in a block of content that is filtered by wpautop.
Author URI: http://www.johannheyne.de
Version: 0.1
*/

add_filter('the_content', 'shortcode_empty_paragraph_fix');
function shortcode_empty_paragraph_fix($content) {   
    $array = array (
        '<p>[' => '[', 
        ']</p>' => ']', 
        ']<br />' => ']'
    );

    $content = strtr($content, $array);

    return $content;
}

// custom tab
// filter prevent loading gallery after save uploaded images
function dt_aftos( $post, $attachments ) {
    if( 'dt_gallery' == get_post_type($_REQUEST['post_id']) ) {
        if( isset($_GET['tab']) && 'type' == $_GET['tab']) {
            unset($_POST['save']);
        }
    }
    return $post;
}
add_filter( 'attachment_fields_to_save', 'dt_aftos', 99, 2 );